package ch.ethz.fcl.listener;

import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

import ch.ethz.fcl.R;
import ch.ethz.fcl.draw.GeoBounds;
import ch.ethz.fcl.googlemap.MetroBuzzGoogleMaps;
import ch.ethz.fcl.udp.Protocal;
import ch.ethz.fcl.udp.UDPClient;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public class OrientationListener implements SensorEventListener {
	private final String TAG = "Orientation Listener";
	private UDPClient client;
	private String msg = "MetroBuzz:Tilt:";
	private SensorManager mSensorManager;
	private Sensor sensor;
	private MapView mapView;

	private float pitch;
	private float roll;
	
    private GeoPoint center = MetroBuzzGoogleMaps.geoPoint1;
    private int lon_min = GeoBounds.minLon;
    private int lon_max = GeoBounds.maxLon;
    private int lat_min = GeoBounds.minLat;
    private int lat_max = GeoBounds.maxLat;

	public OrientationListener(Context mContext) {
		client = new UDPClient();
		mSensorManager = (SensorManager) mContext
				.getSystemService(Context.SENSOR_SERVICE);
		mapView = (MapView)MetroBuzzGoogleMaps.main_view.findViewById(R.id.mapview);
	}

	public void init() {
		List<Sensor> sensors = mSensorManager
				.getSensorList(Sensor.TYPE_ORIENTATION);
		if (sensors.size() > 0)
			sensor = sensors.get(0);

		mSensorManager.registerListener(this, sensor,
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	public void onPause() {
		mSensorManager.unregisterListener(this);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		pitch = event.values[1];
		roll = event.values[2];

		if (pitch < -50 && pitch > -130) {
			Log.d(TAG, "Side TOP");
			client.setMessage(msg + Protocal.SIDE_TOP);
			client.run();
		} else if (pitch > 5 && pitch < 175) {
			Log.d(TAG, "Side Bottom");
			client.setMessage(msg + Protocal.SIDE_BOTTOM);
			client.run();
		} else if (roll > 20) {
			Log.d(TAG, "Side Right");
			client.setMessage(msg + Protocal.SIDE_RIGHT);
			client.run();
		} else if (roll < -20) {
			Log.d(TAG, "Side Left");
			client.setMessage(msg + Protocal.SIDE_LEFT);
			client.run();
		}
		concentrate(mapView);
	}
	
	public void concentrate(MapView mapView) {
		Projection proj = mapView.getProjection();
		GeoPoint gp = proj.fromPixels(0, 0);
		if (gp.getLongitudeE6() < lon_min || gp.getLatitudeE6() > lat_max)
			mapView.getController().setCenter(center);

		gp = proj.fromPixels(1280, 0);
		if (gp.getLongitudeE6() > lon_max || gp.getLatitudeE6() > lat_max)
			mapView.getController().setCenter(center);

		gp = proj.fromPixels(0, 800);
		if (gp.getLongitudeE6() < lon_min || gp.getLatitudeE6() < lat_min)
			mapView.getController().setCenter(center);

		gp = proj.fromPixels(1280, 800);
		if (gp.getLongitudeE6() > lon_max || gp.getLatitudeE6() < lat_min)
			mapView.getController().setCenter(center);
	}
}
